package com.fwj.storm.say;

import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values;

import java.util.Map;
import java.util.Random;

/**
 * @author 傅文江
 * <p>
 * 时间： 2017-11-07 10:17
 * <p>
 * 描述：
 * <p>
 * 版权： Copyright © 2017 新道科技股份有限公司
 **/
public class RandomSpout extends BaseRichSpout {

    private SpoutOutputCollector collector;

    private Random rand;

    private static String[] sentences = new String[]{"朱晓剑:I'm 后台", "过慈双:I'm 前端", "宋丹红:I'm UI", "小明:I'm SB", "灰姑娘:我怕大灰狼"};

    @Override
    public void open(Map conf, TopologyContext context,
                     SpoutOutputCollector collector) {
        this.collector = collector;
        this.rand = new Random();
    }

    @Override
    public void nextTuple() {
        String toSay = sentences[rand.nextInt(sentences.length)];
        this.collector.emit(new Values(toSay));
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("sentence"));
    }

}
